define(['jquery', 'bootstrap', 'backend', 'table', 'form','layui'], function ($, undefined, Backend, Table, Form,layui) {

    var Controller = {

        index: function () {

            layui.use(['element','layer','form'], function() {

                    var element = layui.element;
                    var layer = layui.layer,form = layui.form
                    $ = layui.$;
                    var html = "";
                    //初始化 获取下拉酒店数据
                    Fast.api.ajax({
                        url:'hotels/getHotels',
                    }, function(data,ret){
                        //成功的回调
                        if(ret.code == 1){
                            $.each(ret.rows,function (i,val) {
                                html +='<option value='+ val["hid"] +'>'+ val["h_title"] +'</option>';
                            })
                            $("#sel").append(html);
                            //重新渲染select 否则数据不会显示
                            form.render("select");
                            //等数据样书都加载完成后，再显示，否则样书不全不好看
                            $("#seldiv").show();
                            $("#btndiv").show();
                            $("#roombody").show();


                        }
                        return false;
                    }, function(data,ret){
                        console.log(ret)
                        return false;
                    });



                    //获取房间数据


                    //初始化房间数据
                    getRooms(null);


                    //切换下拉数据
                    form.on('select(selecthotel)', function(data){
                        var hotelid=data.value;
                        getRooms(hotelid);
                        //关闭右侧
                        var sideBar = $("#sideBar");
                        $("#sideBar").removeClass("addWidth");
                        $("#roombody").css("width","100%");
                    });



                });

              //获取房间数据
              function getRooms(hotelId){
                var htmlroom = ""; //这里必须用局部变量 不能全局 否则数据empty 也没用，本身缓存组件缓存了，注意这点否则浪费很多时间
                Fast.api.ajax({
                    url:'house/getRooms',
                    data:{hotelId:hotelId}
                }, function(data, ret){

                    if(ret.code == 1){
                        $.each(ret.rows,function (i,val) {

                            htmlroom += '<div class="rommline">\n' +
                                '<legend>'+ val["storeyHtml"] +'</legend>\n';

                            $.each(val['storeydata'],function (ii,vv) {
                                if(vv["h_statusText"] === "normal"){
                                    htmlroom +=  '<button type="button" class="layui-btn diy_normal showright" index="'+ vv["id"] +'"  style="margin: 10px;background: #5FB878;">\n' +
                                        '               <i class="layui-icon">&#xe68e;</i>\n' +
                                        '               <a href="javascript:;" title="查看详情" style="color: white">\n' +
                                        '                     '+ vv["h_title"]  +' '+
                                        '               </a>\n' +
                                        '          </button>\n' ;
                                }else if(vv["h_statusText"] === "using"){
                                    htmlroom +=   '<button type="button" class="layui-btn layui-btn-normal diy_using showright" index="'+ vv["id"] +'"  style="margin: 10px;">\n' +
                                        '               <i class="layui-icon">&#xe66f;</i>\n' +
                                        '               <a href="javascript:;" title="查看详情" style="color: white">\n' +
                                        '                     '+ vv["h_title"]  +' '+
                                        '               </a>\n' +
                                        '         </button>\n';
                                }else if(vv["h_statusText"] === "reserve"){
                                    htmlroom += '<button type="button" class="layui-btn layui-btn-normal diy_reserve showright" index="'+ vv["id"] +'"  style="margin: 10px;background: #FFB800">\n' +
                                        '                <i class="layui-icon">&#xe68d;</i>\n' +
                                        '                <a href="javascript:;" title="查看详情" style="color: white">\n' +
                                        '                     '+ vv["h_title"]  +' '+
                                        '                </a>\n' +
                                        '        </button>';

                                }else if(vv["h_statusText"] === "repair"){
                                    htmlroom +=  '<button type="button" class="layui-btn layui-btn-normal diy_repair showright" index="'+ vv["id"] +'"  style="margin: 10px; background: #FF5722;">\n' +
                                        '               <i class="layui-icon">&#xe631;</i>\n' +
                                        '               <a href="javascript:;" title="查看详情" style="color: white">\n' +
                                        '                     '+ vv["h_title"]  +' '+
                                        '               </a>\n' +
                                        '        </button>\n';
                                }else if(vv["h_statusText"] === "cleaning"){
                                    htmlroom +=   '<button type="button" class="layui-btn layui-btn-normal diy_cleaning showright" index="'+ vv["id"] +'"  style="margin: 10px;background:mediumvioletred;">\n' +
                                        '              <i class="layui-icon">&#xe636;</i>\n' +
                                        '               <a href="javascript:;" title="查看详情" style="color: white">\n' +
                                        '                     '+ vv["h_title"]  +' '+
                                        '               </a>\n' +
                                        '           </button>\n';
                                }else if(vv["h_statusText"] === "hidden"){
                                    htmlroom +=   '<button type="button" class="layui-btn layui-btn-normal diy_hidden showright" index="'+ vv["id"] +'" style="margin: 10px;background: #393D49">\n' +
                                        '                   <i class="layui-icon">&#x1007;</i>\n' +
                                        '                   <a href="javascript:;"  title="查看详情"  style="color: white">\n' +
                                        '                     '+ vv["h_title"]  +' '+
                                        '                   </a>\n' +
                                        '              </button>\n';
                                }

                            })

                            htmlroom += '</div>';

                        });
                    }


                    $("#roombody").empty().append(htmlroom);

                    $(document).on('click',".showright",function(e){
                        if(!e.isPropagationStopped()){
                            var id = $(this).attr("index")
                            showRightWall(id)
                        }
                        e.stopPropagation();

                    });
                    return false;

                }, function(data, ret){
                    $("#roombody").empty()

                    return false;
                });
            }


                //自定义事件 修改btn颜色
                $(document).on("click", "#btndiv .layui-input-block div", function () {
                    var index = $(this).index();
                    if($(this).hasClass("layui-form-checked")){

                        if(index == 1){
                            console.log("空闲")
                            //空闲
                            $(this).children("span").css("background","#5FB878");
                            $(".diy_normal").css("display","inline-block");
                        }else if(index == 3){
                            //使用
                            $(this).children("span").css("background","#1E9FFF");
                            $(".diy_using").css("display","inline-block");
                        }else if(index == 5){
                            //预定
                            $(this).children("span").css("background","#FFB800");
                            $(".diy_reserve").css("display","inline-block");
                        }else if(index == 7){
                            //打扫
                            $(this).children("span").css("background","mediumvioletred");
                            $(".diy_cleaning").css("display","inline-block");
                        }else if(index == 9){
                            //维修
                            $(this).children("span").css("background","#FF5722");
                            $(".diy_repair").css("display","inline-block");
                        }else if(index == 11){
                            //隐藏
                            $(this).children("span").css("background","#393D49");
                            $(".diy_hidden").css("display","inline-block");
                        }
                    }else{

                        if(index == 1){
                            //空闲
                            $(this).children("span").css("background","#d2d2d2");
                            $(".diy_normal").css("display","none");
                        }else if(index == 3){
                            //使用
                            $(this).children("span").css("background","#d2d2d2");
                            $(".diy_using").css("display","none");
                        }else if(index == 5){
                            //预定
                            $(this).children("span").css("background","#d2d2d2");
                            $(".diy_reserve").css("display","none");
                        }else if(index == 7){
                            //打扫
                            $(this).children("span").css("background","#d2d2d2");
                            $(".diy_cleaning").css("display","none");
                        }else if(index == 9){
                            //维修
                            $(this).children("span").css("background","#d2d2d2");
                            $(".diy_repair").css("display","none");
                        }else if(index == 11){
                            //隐藏
                            $(this).children("span").css("background","#d2d2d2");
                            $(".diy_hidden").css("display","none");
                        }


                        //$(this).children("span").css("background","#d2d2d2");

                    }
                });


                $(document).on("click","#hideright",function () {
                    var sideBar = $("#sideBar");
                    $("#sideBar").removeClass("addWidth");
                    $("#roombody").css("width","100%");

                })

            //显示右侧面板
            function showRightWall(id){
                var sideBar = $("#sideBar");
                //我的逻辑上是先判断没有这个class，没有的话就添加，这个添加动作会触发宽度加长的过渡效果
                if (!sideBar.hasClass("addWidth")) {
                    $("#sideBar").addClass("addWidth");
                    $("#roombody").css("width","80%");
                }
                //同理，展开之后要切换，移除这个class来触发减小宽度的过渡效果
                else {
                    //$("#sideBar").removeClass("addWidth");
                    //$("#roombody").css("width","100%");
                }

                $.ajax({
                    type: "GET",
                    url: "rooms/getRoomInfo",
                    data: {id:id},
                    dataType: "json",
                    success: function(data){

                        var datainfo = data.data;
                        $("#statusid").val(datainfo.id)
                        $("#statushtitle").val(datainfo.htitle)
                        $("#statustitle").val(datainfo.h_title)
                        $("#statuscode").val(datainfo.hstorey + datainfo.h_unit +" "+ datainfo.h_housebrand)
                        $("#statusprice").val(datainfo.h_price)
                        $("#statusallprice").val(datainfo.h_allprice)
                        $("#beforeTime").val(datainfo.beforeTime)

                        if(datainfo.h_statusText == "reserve"){
                            $("#reserveStatus").css("display","block");
                        }else{
                            $("#reserveStatus").css("display","none");
                        }

                        //动态设置select 选项值
                        var select = 'dd[lay-value=' + datainfo.h_statusText + ']';
                        $('#statusText').siblings("div.layui-form-select").find('dl').find(select).click();

                        var moveInfo = data.data.moveInfo; //入住信息
                        if(moveInfo == ''){
                             $("#moveIntoInfo").css("display","none");
                        }else{
                             //渲染信息
                             $("#moveIntoInfo .classAddtime").html(moveInfo.add_time)
                             $("#moveIntoInfo .classUser").html(moveInfo.nickName)
                             $("#moveIntoInfo .classStime").html(moveInfo.stime)
                             $("#moveIntoInfo .classMtime").html(moveInfo.moveinto_time)
                             $("#moveIntoInfo .classEtime").html(moveInfo.etime)
                             $("#moveIntoInfo .classAllmoney").html(moveInfo.all_money)
                             $("#moveIntoInfo").css("display","block");
                        }

                        var reserveList = data.data.reserveList; //预订信息
                        $("#reserveInfo").empty();//先清空预约数据
                        var relen = reserveList.length;

                        var reservehtml = "";
                        var selectHtml = "";
                        if(relen > 0){
                            for(var i= 0;i<relen;i++){

                                reservehtml += '<ul style="position: relative"><div class="userselect" style="position: absolute;left: -40%;top: 45%;" ><input type="radio" name="user_ordernumber" value="'+ reserveList[i]["order_number"] +'" ';
                                if(i == 0){
                                    reservehtml += "checked";
                                }

                                    reservehtml += '> </div>\n' +
                                    '                                                    <li>订单时间：<span>'+ reserveList[i]["add_time"] +'</span></li>\n' +
                                    '                                                    <li>预订用户：<span>'+ reserveList[i]["nickName"] +'</span></li>\n' +
                                    '                                                    <li>预订时间：<span class="classStime">'+ reserveList[i]["stime"] +'</span></li>\n' +
                                    '                                                    <li>入住时间：<span>'+ reserveList[i]["moveinto_time"] +'</span></li>\n' +
                                    '                                                    <li>退房时间：<span class="classEtime">'+ reserveList[i]["etime"] +'</span></li>\n' +
                                    '                                                    <li>订单状态：<span>预订中</span></li>\n' +
                                    '                                                    <li>已缴费用：<span>'+ reserveList[i]["all_money"] +'</span>元</li>\n' +
                                    '\n' +
                                    '                                                </ul>\n' +
                                    '                                                <hr>';
                                }


                            //渲染入住人员信息
                            selectHtml += ' <option value="using">人员二</option>';



                        }

                        $(".user_ordernumber:first").attr("checked",true);
                        $("#reserveInfo").append(reservehtml);
                        $("#reservePeople").append(selectHtml);

                        layui.use(['element','layer','form'], function() {

                            var form = layui.form;


                            form.render("radio");
                            form.render("select");

                        });








                    }
                });

            }

            //修改状态
           $("#changeStatusText").click(function () {
               var hotelid = $("#sel").val();
               var id = $("#statusid").val();
               var statusText = $("#statusText").val();
               var order_number  = $("input[name='user_ordernumber']:checked").val();

               layer.confirm('是否确定修改房间状态?', {icon: 3, title:'状态更新'}, function(index){

                   $.ajax({
                       type: "GET",
                       url: "rooms/changeRoomStatus",
                       data: {id:id,statusText:statusText,order_number:order_number},
                       dataType: "json",
                       success: function(data){
                           if(data.code == 1){
                               layer.msg(data.msg,{time:2000,icon: 6})
                               getRooms(hotelid);
                               //关闭右侧
                               var sideBar = $("#sideBar");
                               $("#sideBar").removeClass("addWidth");
                               $("#roombody").css("width","100%");
                           }else{
                               layer.msg(data.msg,{time:2000,icon: 5})
                                return false;
                           }

                       }
                   });

                   layer.close(index);
               });

           })



        },

    };
    return Controller;
});
